<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\GoodsLogistic;
use App\Models\ShippingAddress;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Http\Request;

class GoodsLogisticController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $good_order_id = request('good_order_id',0);
        return Grid::make(new GoodsLogistic(), function (Grid $grid)use($good_order_id)  {
            $grid->model()->where('good_order_id', '=', $good_order_id);
            $grid->model()->orderBy('time');

            //$grid->column('id')->sortable();
            $grid->column('message')->display(function ($var){



                if(!empty($this->messagesystem)){
                    $msg = trans('validation.logistics',[],config('zh-CN'))[$this->messagesystem];
                    if($this->messagesystem == "arrival"){
                        $shipping_address = ShippingAddress::getAdd($this->shipping_address_id);
                        $msg = sprintf($msg,$shipping_address['address']);
                    }
                    return $msg;
                }else{
                    return $var;
                }

            });
            $grid->column('time');
            $grid->quickCreate(function (Grid\Tools\QuickCreate $create)use($good_order_id) {
                $create->hidden('good_order_id')->default($good_order_id);
                $create->datetime('time')->default(now())->required();
                $create->text('message')->width(300);
                $create->select('messagesystem',"选择物流信息（选择或填入，选择优先）")->width(300)->options(self::getmsg());
                $create->select('shipping_address_id',"选择一个目的地（该地址替代文案中的%s）")->width(300)->options(self::shipping_address());
            });
            $grid->disableViewButton();
            $grid->disableCreateButton();
        });
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new GoodsLogistic(), function (Form $form) {
            $good_order_id = request('good_order_id',0);
            $form->text('good_order_id');
            $form->text('message');
            $form->text('messagesystem');
            $form->text('shipping_address_id');
            $form->datetime('time')->default(now());

            $form->submitted(function (Form $form) {
                if(empty($form->input('message')) && empty($form->input('messagesystem'))){
                    return $form->response()->error('必须填入或选择物流信息~');
                }

                if($form->input('messagesystem') == 'arrival' &&  empty($form->input('shipping_address_id'))){
                    return $form->response()->error('必须选择到货地址~');
                }


            });

            /*
            $form->saving(function (Form $form) {
                // 判断是否是新增操作
                $messageadd = $form->messageadd;
                if(!empty($messageadd)){
                    $form->message = $messageadd;
                }

                // 删除用户提交的数据
                $form->deleteInput('messageadd');

            });
            */
            $form->saved(function (Form $form) {
                return $form->response()->success('保存成功')->refresh();
            });

        });
    }


    public function getmsg(){
        //$msgarr = trans('validation.logistics',[],config('app.api_locale'));
        $msgarr_zh = trans('validation.logistics',[],config('zh-CN'));

        $data = [];
        foreach ($msgarr_zh as $k=>&$v){
            /*
            if($k == "arrival"){
                $address = "";
                $shipping_address = ShippingAddress::query()->inRandomOrder()->first();
                if($shipping_address){
                    $address = $shipping_address->address;
                }
                $v = sprintf($v,$address);
                $msgarr_zh[$k] = sprintf($msgarr_zh[$k],$address);
            }
            */
            //$data[$v] = $msgarr_zh[$k];

        }
        return $msgarr_zh;
    }


    public function shipping_address(){
        $add = ShippingAddress::query()->get()->pluck('address','id');
        return $add;
    }

}
